package com.ftcs.controller.common.service.impl;

import com.ftcs.controller.common.service.ActivitiNormalService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

@Slf4j
@Service(value = "activitiNormalService")
public class ActivitiNormalServiceImpl implements ActivitiNormalService {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;


    @Override
    public boolean devlopActiviti(String folderPath, HashMap<String, String> map) {
        // 创建一个部署构建器,从类路径一次只能添加一个文件
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource(folderPath + File.separator + map.get("id") + ".bpmn")
                .addClasspathResource(folderPath + File.separator + map.get("id") + ".png")
                .name(map.get("name")).category(map.get("category")).deploy();

        log.info("流程名称【 {}】", deploy.getName());
        log.info("流程id【{}】", deploy.getId());
        log.info("流程类别【{}】", deploy.getCategory());

        return true;
    }

    @Override
    public boolean deployActivitiByZip(String folderPath, HashMap<String, String> map) {
        InputStream in = this.getClass().getClassLoader()
                .getResourceAsStream("folderPath" + File.separator + map.get("id") + ".bpmn");
        ZipInputStream zipInputStream = new ZipInputStream(in);
        Deployment deploy = repositoryService.createDeployment()// 创建一个部署构建器
                .addZipInputStream(zipInputStream).name(map.get("name")).category(map.get("category")).deploy();

        log.info("流程名称【 {}】", deploy.getName());
        log.info("流程id【{}】", deploy.getId());
        log.info("流程类别【{}】", deploy.getCategory());
        return true;
    }

    @Override
    public boolean delegateTask(String taskId, String loginName) {
        taskService.delegateTask(taskId, loginName);
        return true;
    }

    @Override
    public boolean resolveTask(String taskId, Map<String, Object> map) {
        taskService.resolveTask(taskId, map);
        return true;
    }
}
